package com.example.onlinemusic.controller;

import com.example.onlinemusic.common.R;
import com.example.onlinemusic.model.request.SongRequest;
import com.example.onlinemusic.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.MultipartConfigElement;

/**
 * 歌曲管理控制层
 */
@RestController
public class SongController {

    @Autowired
    SongService songService;

    /**
     * 根据歌手Id查询歌曲列表
     *
     * @param id
     * @return
     */
    @GetMapping("/song/singer/detail")
    public R selectSongsBySingerId(@RequestParam int singerId) {
        return songService.selectSongsBySingerId(singerId);
    }

    /**
     * 更新歌曲
     *
     * @param songRequest
     * @return
     */
    @PostMapping("/song/update")
    public R updateSong(@RequestBody SongRequest songRequest) {
        return songService.updateSong(songRequest);
    }

    /**
     * 删除歌曲
     *
     * @param id
     * @return
     */
    @DeleteMapping("/song/delete")
    public R deleteSong(@RequestParam int id) {
        return songService.deleteSong(id);
    }

    /**
     * 添加歌曲
     *
     * @param songRequest
     * @return
     */
    @PostMapping("/song/add")
    public R addSong(SongRequest songRequest, @RequestParam("file") MultipartFile mpfile) {
        return songService.addSong(songRequest, mpfile);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
        factory.setMaxFileSize(DataSize.of(20, DataUnit.MEGABYTES));
        // 设置总上传数据总大小10M
        factory.setMaxRequestSize(DataSize.of(20, DataUnit.MEGABYTES));
        return factory.createMultipartConfig();
    }

    /**
     * 更新歌曲图片
     *
     * @param urlFile
     * @param id
     * @return
     */
    @PostMapping("/song/img/update")
    public R updateSongPic(@RequestParam("file") MultipartFile urlFile, @RequestParam("id") int id) {
        return songService.updateSongPic(urlFile, id);
    }

    /**
     * 首页模块:查询所有歌曲
     *
     * @return
     */
    @GetMapping("/song")
    public R allSong() {
        // TODO :查询所有歌曲
        return songService.allSong();
    }

    /**
     * 根据name查询歌曲信息
     *
     * @param name
     * @return
     */
    @GetMapping("/song/singerName/detail")
    public R songOfSingerName(@RequestParam String name) {
        return songService.songOfSingerName(name);
    }


    /**
     * 根据id查询歌曲信息
     * @param id
     * @return
     */
    @GetMapping("/song/detail")
    public R getSongDetail(@RequestParam  int id){
        return songService.getSongDetail(id);
    }

    /**
     * 判断歌曲是否已支付
     * @param songId
     * @param userId
     * @return
     */
    @GetMapping("/song/isPay")
    public R isPay(@RequestParam("songId") Integer songId,
                   @RequestParam("userId") Integer userId)  {
        return songService.isPay(songId,userId);
    }

}
